<?php

/**
 * Framework_Module_User_Watch
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Module
 * @filesource
 */

/**
 * Framework_Module_User_Watch
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @package Module
 */
class Framework_Module_User_Watch extends Framework_Module_User
{
		
	/**
	 * $serializerOptions
	 *
	 * @access public
	 * @var mixed $serializerOptions
	 */
	public $serializerOptions = null;

	/**
     * __construct
     *
     * Initializes the module: sets the default presenter, the allowed
     * conrollers, and the allowed events
     *
     * @access 		public
     * @return 		void
     */
	public function __construct()
	{
		parent::__construct();
		$this->presenter = 'Module';
		$this->validEvents[] = 'info';
		$this->validEvents[] = 'redirect';
	}

	/**
     * __default
     * 
     * Do the watch
     *
     * @access 		public
     * @return 		mixed 		boolean true on success, PEAR_Error on failure
     */
	public function __default()
	{
		// At this point, flag the watch
		if ($this->user->userStatus == NORMAL_USER_STATUS){
			if (!isset($_GET['cancel'])) {
				$owner = Framework_Owner::singleton();
				$friendStatusTable = Framework_DB::tableFactory('FriendStatus');
				$friendStatusTable->load(array('idOwner' => $owner->idOwner, 'idSiteUser' => $this->user->idSiteUser));
				$friendStatusTable->idOwner = $owner->idOwner;
				$friendStatusTable->idSiteUser = $this->user->idSiteUser;
				if (!$friendStatusTable->watchingUser) {
					$friendStatusTable->watchingUser = 0;
				}
				$friendStatusTable->watchedByUser = 1;
				$result = $friendStatusTable->save();
				if (PEAR::isError($result)) {
					$errors[] = 'Database error: '.$result->getMessage();
				}
			} 
		} else {
			$errors[] = 'Must be logged in to watch';
		}

		if($errors) {
			$this->setData('errors',$errors);
		}
	  
	  
		// set template
		if (Framework::$request->isAjax())
			$this->tplFile = 'watchAjax.tpl';
		else {
			if (isset($_GET['viaFrame'])) {
				$this->tplFile = 'WatchModal.tpl';
				return true;
			} else if (isset($_GET['from'])) {
				Framework_Header::redirect($_GET['from']);
			} else if (!$errors) {
				Framework_Header::redirect('http://'.$owner->siteDomain.'/'.$owner->userName);
			}
		}
	  
		return true;
	}
	
	/**
     * redirect
     *
     *
     * @access 		public
     * @return 		mixed 		void, PEAR_Error on failure
     */
	public function redirect()
	{
		$owner = Framework_Owner::singleton();
		// From needs to be referrer, or is it needed at all?
		if (!isset($_GET['from'])) {
			return PEAR::raiseError('Invalid Request: Missing "from" parameter');
		} else {
			$from = htmlentities($_GET['from']);
			Framework_Header::redirect('http://'.$this->user->siteDomain.'/'.$this->user->userName.'/User/WatchUnlock/viaRedirect', array('from' => $from, 'remoteOwner' => $owner->loginName.'@'.$owner->siteDomain));
		}
		
	}
	 
	/**
     * info
     *
     * Generates XML response with remote SiteUser info
     *
     * @access 		public
     * @return 		mixed 		void, PEAR_Error on failure
     */
	public function info()
	{
		$this->presenter = 'REST';
		$this->serializerOptions = array (
		'indent' => '  ',
		'rootName' => 'peopletab',
		'defaultTagName' => 'message');
		$owner = Framework_Owner::singleton();
		if ($owner->isValid) {
			$this->setData('header',
			array('type' => 'watchInfo')
			);

			$this->setData('message',
			array('avatarURL' => ($owner->avatarURL ? $owner->avatarURL : ' '),
			'blurb' => ($owner->blurb ? $owner->blurb : ' '),
			'publicKey' => $owner->publicKey
			)
			);

		} else {
			$this->makeError('Invalid Parameters: '.$_SERVER['PATH_INFO']);
		}
	}
	 
	/**
     * makeError
     *
     * Generate an XML error
     *
     * @access 		private
     * @param		string		$errorMessage
     * @return 		void
     */
	private function makeError($errorMessage)
	{
		$this->setData('header',
		array('type' => 'error')
		);

		$this->setData('message',
		array('errorMessage' => $errorMessage)
		);

	}
}

?>